package com.umfb.edu.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.umfb.commons.exception.OperateException;
import com.umfb.constant.State;
import com.umfb.demo.entity.edu.ColumnInfo;
import com.umfb.demo.service.edu.IColumnInfoService;
import com.umfb.edu.mapper.ColumnInfoMapper;
import com.umfb.utils.IdWorker;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by zcah on 18-7-13.
 */
@Service
public class ColumnInfoServiceImpl implements IColumnInfoService {
    @Autowired
    private ColumnInfoMapper columnInfoMapper;

    @Transactional(readOnly=false,rollbackFor=Exception.class)
    @Override
    public void addClomnInfos(ColumnInfo columnInfo) throws Exception {
        Date nowDate = new Date();
        columnInfo.setColumnId(IdWorker.getIdWorker().nextId());
        columnInfo.setCreateAt(nowDate);
        columnInfo.setState(State.UNDELETE.getStateNum());

        columnInfoMapper.insert(columnInfo);
    }

    @Transactional(readOnly=false,rollbackFor=Exception.class)
    @Override
    public List<ColumnInfo> findColumnBycCourseName(String courseName) throws Exception {
        if(StringUtils.isBlank(courseName)){
            try {
                throw new OperateException("根据课程名称查询栏目时courseName为空"+"----[" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + "]");

            }catch (Exception e){
                return null;
            }
        }
        List<ColumnInfo> columnInfoList = columnInfoMapper.findColumnBycCourseName(courseName);

        return columnInfoList;
    }

    /**
     * 动态分页查询栏目列表
     * @param start 页数
     * @param length 每页记录条数
     * @param paraMap 动态查询参数
     * @return
     */
    @Transactional(readOnly=false,rollbackFor=Exception.class)
    @Override
    public PageInfo<ColumnInfo> findColumnListBySelective(int start, int length, Map<String, Object> paraMap) throws Exception {
        PageHelper.startPage(start,length);
        List<ColumnInfo> columList=columnInfoMapper.findColumnListBySelective(paraMap);
        return new PageInfo(columList);
    }
}
